'use strict';
var initDb = require('./utils/initDb');
initDb();
var tadel = require('./models/tadel')

// var seq=require('./db');

var Crawler = require("crawler");

var c = new Crawler({
    rateLimit: 10,
    maxConnections: 1,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;

            var list = $('.dw_tadel .el').toArray();

            list.forEach(element => {


                var txt = $(element).find('.t1 a').attr('title');

                var txt1 = $(element).find('.t2 a').text().trim().replace(/[ \n\r]/g, '');

                var txt2 = $(element).find('.t3').text().trim().replace(/[ \n\r]/g, '');;

                var txt3 = $(element).find('.t4').text().trim().replace(/[ \n\r]/g, '');;

                var txt4 = $(element).find('.t5').text().trim().replace(/[ \n\r]/g, '');;

                tadel.create({ Title: txt, Company: txt1, Region: txt2, Salary: txt3, Time: txt4 });
                // console.log(txt);

            })

            // console.log($("title").text());
        }
        done();
    }
});
for (let index = 1; index <= 25; index++) {

    c.queue('https://search.51job.com/list/010000%252C180200%252C020000%252C030200%252C040000,000000,7501,00,9,99,%2520,1,' + index + '.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
}
